// pages/myApplication/myApplication.js
var time= require("../../utils/util.js");
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data:{
    timeState:[],
    winHeight:"",//窗口高度
    currentTab:0, //预设当前项的值
    scrollLeft:0, //tab标题的滚动条位置
    getMyApplications:[],//参与的全部招募
    isShow:[] ,//是否显示出来
    startTime:[],//开始时间,将时间戳转化为标准日期 //时间戳转为日期   let dataTime = time.formatTimeTwo(timeTamp,'Y/M/D')
    },

    // 滚动切换标签样式
    switchTab:function(e){
      // console.log("触发了switchTab" , e)
      this.setData({
          currentTab:e.detail.current
      });
      this.checkCor();
      console.log('id',e.detail.current)
      // console.log('id',e.detail.current)
      const getMyApplications = wx.getStorageSync('getMyApplications')
      // const joinRecruitAll = wx.getStorageSync('joinRecruitAll')
      let  isShow=[] 
      if(e.detail.current=='0'){
        for (let index = 0; index < getMyApplications.length; index++) {
          isShow.push('block') //显示
        }
        this.setData({
          isShow:isShow
        })
      }else if(e.detail.current=='1'){  //已通过
        for (let index = 0; index < getMyApplications.length; index++) {
          console.log('状态',getMyApplications[index].applicationStatus)
          if(getMyApplications[index].applicationStatus==1){
            isShow.push('block') //显示
            console.log('运行了显示')
          }else{
            console.log('运行了隐藏')
            isShow.push('none') //隐藏
          }
        }
        this.setData({
          isShow:isShow
        })
      }else if(e.detail.current=='2'){  //未通过
        for (let index = 0; index < getMyApplications.length; index++) {
          if(getMyApplications[index].applicationStatus==2){
            isShow.push('block') //显示 
          }else{
            isShow.push('none') //隐藏
          }
        }
        this.setData({
          isShow:isShow
        })
      }else{
        for (let index = 0; index < getMyApplications.length; index++) {
          if(getMyApplications[index].applicationStatus==0){
            isShow.push('block') //显示 
          }else{
            isShow.push('none') //隐藏
          }
        }
        this.setData({
          isShow:isShow
        })
      }
  },
  // 点击标题切换当前页时改变样式
  swichNav:function(e){
    // console.log("触发了swichNav+++++",e )
      var cur=e.target.dataset.current;
      if(this.data.currentTaB==cur){return false;}
      else{
          this.setData({
              currentTab:cur
          })
      }
      console.log('++++++id',e.target.dataset.current)
      // const joinRecruitAll = wx.getStorageSync('joinRecruitAll')
      const getMyApplications = wx.getStorageSync('getMyApplications')
      const applicantAll = wx.getStorageSync('applicantAll')
      // console.log('applicantAll',getMyApplications)
      let  isShow=[] 
      if(e.target.dataset.current=='0'){
        for (let index = 0; index < getMyApplications.length; index++) {
          isShow.push('block') //显示
        }
        this.setData({
          isShow:isShow
        })
      }else if(e.target.dataset.current=='1'){  //已通过
        for (let index = 0; index < getMyApplications.length; index++) {
          console.log('申请状态',getMyApplications[index].applicationStatus)
          if(getMyApplications[index].applicationStatus==1){
            isShow.push('block') //显示
            console.log('运行了显示')
          }else{
            console.log('运行了隐藏')
            isShow.push('none') //隐藏
          }
        }
        this.setData({
          isShow:isShow
        })
      }else if(e.target.dataset.current=='2'){  //未通过
        for (let index = 0; index < getMyApplications.length; index++) {
          if(getMyApplications[index].applicationStatus==2){
            isShow.push('block') //显示 
          }else{
            isShow.push('none') //隐藏
          }
        }
        this.setData({
          isShow:isShow
        })
      }else{  // 审核中
        for (let index = 0; index < getMyApplications.length; index++) {
          if(getMyApplications[index].applicationStatus==0){
            isShow.push('block') //显示 
          }else{
            isShow.push('none') //隐藏
          }
        }
        this.setData({
          isShow:isShow
        })
      }

  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor:function(){
    if (this.data.currentTab>4){
      this.setData({
        scrollLeft:300
      })
    }else{
      this.setData({
        scrollLeft:0
      })
    }
  },
  
  // 判断招募是否正在进行
  isRecruitActivity:function(){
    const getMyApplications = wx.getStorageSync('getMyApplications')
    console.log('getMyApplications',getMyApplications)
    let  timeState=[] 
    let startTime=[]
    for (let index = 0; index < getMyApplications.length; index++) {
      let dataTime = time.formatTimeTwo(getMyApplications[index].recruit.startTime,'Y.M.D')
      startTime.push(dataTime)
     if( Date.parse(new Date())/1000-getMyApplications[index].recruit.endTime>0) {
      //  已结束
      timeState.push('已结束')
     }else{
      timeState.push('进行中')
     }
    }
    this.setData({
      startTime:startTime,
      timeState:timeState
    })
  },

  // 跳转到详情页面
  goDetails:function (e) {
    console.log(e)
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/details/details?id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const getMyApplications = wx.getStorageSync('getMyApplications')
    if(getMyApplications){
      console.log('调用缓存的数据')
      this.setData({
        getMyApplications:getMyApplications
      })
      this.isRecruitActivity() //判断招募是否结束
    }else{
      app.getApplications().then(res=>{
        this.setData({
          getMyApplications:getMyApplications
        })
        this.isRecruitActivity() //判断招募是否结束
      }) 
    }
    // console.log('数据',this.data.joinRecruitAll)
    var that = this; 
        //  高度自适应
        wx.getSystemInfo( {  
            success: function( res ) {  
                var clientHeight=res.windowHeight,
                    clientWidth=res.windowWidth,
                    rpxR=750/clientWidth;
              var  calc=clientHeight*rpxR-180;
                console.log(calc)
                that.setData( {  
                    winHeight: calc  
                });  
            }  
        });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  onRefresh(){
    wx.showLoading({
      title: '刷新中...',
    })
    this.getData();
    //隐藏loading 提示框
  },
  // 下拉刷新  获取数据
  getData(){
    
    app.getRecruitAll().then(res=>{
      this.setData({
        recruitAll:wx.getStorageSync('recruitAll')
      })
      this.isRecruitActivity()
      wx.hideLoading();
    }).catch(err=>{
      console.log('下拉刷新失败',err)
      wx.hideLoading();
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})